---
title: "Analysis for Collaboration Uganda"
author: "Eric A. Coleman"
date: '2022-05-02'
output: html_document
---

```{r}
library(foreign)
library(grf)
long.uga <- read.dta("../data/longuga.dta")
long.uga <- long.uga[!(is.na(long.uga$collab_demean)),]


did <- (long.uga$treat=="Treatment")*(long.uga$endline=="Endline")
W.hat <- long.uga$meandid
village <- as.numeric(long.uga$village)
collab <- as.numeric(long.uga$collab_demean)

X <- data.frame(long.uga$idnum, long.uga$train, collab, did, W.hat, village, long.uga$issueranking_land, long.uga$issueranking_soc, as.numeric(long.uga$agem), long.uga$gender, long.uga$socialnetwork, as.numeric(long.uga$hhsize), long.uga$psclacc, as.numeric(long.uga$visitin), as.numeric(long.uga$visitout), long.uga$ownland, long.uga$hcacc)
X <- X[complete.cases(X),]

train.x <- subset(X,long.uga.train==1)
test.x <- subset(X,long.uga.train==0)

cf <- causal_forest(Y= train.x$collab,
          W=train.x$did,
          W.hat=train.x$W.hat,
          X=model.matrix(~ . -1, data=train.x[, 7:ncol(X)]),
          clusters=train.x$village,
          num.trees = 10000,
          seed = 1259
          )

summary(cf)
average_treatment_effect(cf, target.sample = "all")
average_treatment_effect(cf, target.sample = "treated")

var_imp <- variable_importance(cf)
var_names <- c("Issue Ranking - Land", "Issue Ranking - Social Services", "Age", "Male", "Female", "Social Network User", "Household Size", "Access to Primary School", "Visit In Freq", "Visit Out Freq", "Owns Land", "Access to Health Clinic")
importance <- data.frame(cbind(var_names, var_imp))
names(importance)[1] <- "variable"
names(importance)[2] <- "importance"
importance
readstata13::save.dta13(importance, "../data/importance.dta")

test_calibration(cf)

pred_train <- predict(
    object=cf,
    estimate.variance = TRUE
)

preds <- predict(
    object=cf,
    estimate.variance = TRUE,
    newdata = model.matrix(~ ., data=X[, 7:ncol(X)])
)

out <- cbind(preds, X$long.uga.idnum)
names(out)[1] <- "tau"
names(out)[2] <- "tause"
names(out)[3] <- "idnum"
hist(out$tau)

readstata13::save.dta13(out, "../data/temp_grf_merge.dta")

```

